package com.example.demo.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.bean.SCustomer;
import com.example.demo.common.utils.*;
import com.example.demo.service.SCustomerService;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(tags="顾客信息服务")
@RestController
@RequestMapping("/customer")
public class SCustomerController {
	@Autowired
	SCustomerService scusService;
	
	@GetMapping("/all")
	@ApiOperation(value = "查找所有顾客信息",notes="分页显示所有顾客信息")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "pageNum",value = "页码",defaultValue = "1"),	@ApiImplicitParam(name="pageSize",value = "每页显示个数",defaultValue = "3")	
	})
	public Response findAll(@RequestParam(defaultValue = "1")int pageNum,@RequestParam(defaultValue = "3")int pageSize) {
		PageInfo<SCustomer> page=scusService.findAllByPage(pageNum, pageSize);
		return Response.success(page);
	}
	
	@GetMapping("/name")
	@ApiOperation(value = "按名字查找顾客信息",notes = "分页显示所有顾客信息")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "pageNum",value = "页码",defaultValue = "1"),	@ApiImplicitParam(name="pageSize",value = "每页显示个数",defaultValue = "3")	
	})
	public Response findByName(String name,@RequestParam(defaultValue = "1")int pageNum,@RequestParam(defaultValue = "3")int pageSize) {
		PageInfo<SCustomer> page=scusService.findByName(name, pageNum, pageSize);
		return Response.success(page);
	}
	@PostMapping("/saveorupdate")
	@ApiOperation(value = "保存或更新顾客信息")
	public Response saveOrUpdate(SCustomer sc) {
		scusService.saveOrUpdate(sc);
		return Response.success(sc.getId()==null?"添加顾客信息成功":"保存顾客信息成功");
	}
	@DeleteMapping("/del/by/id")
	@ApiOperation(value = "根据id删除")
	public Response deleteById(Integer id) {
		scusService.deleteById(id);
		return Response.success("删除id为"+id+"的顾客信息成功");
	}
	@DeleteMapping("/batch/del")
	@ApiOperation(value = "批量删除")
	 public Response batchDeleteById(int [] id) {
		 scusService.batchDeleteById(id);
		 return Response.success("批量删除成功");
	 }
	
	
	
}
